package com.hnluchuan.staff.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.hnluchuan.staff.dto.EmployeeDTO;
import com.hnluchuan.staff.dto.FamilyDTO;
import com.hnluchuan.staff.dto.UserDTO;
import com.hnluchuan.staff.service.EmployeeService;
import com.hnluchuan.utils.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.staff.dto.EmergencyContactDTO;
import com.hnluchuan.staff.service.EmergencyContactService;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.staff.web.controller.BaseController;

import javax.jws.soap.SOAPBinding;

@Controller
@RequestMapping(value = "/admin/emergencyContact", produces="text/html;charset=UTF-8")
public class AdminEmergencyContactController extends BaseController {
	private UserDTO userDTO;
	private EmployeeDTO employeeDTO;

	@Autowired
	private EmergencyContactService emergencyContactService;

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		UserDTO userDTO=getCurrentUserDTO();
		Integer type=userDTO.getType();
		map.put("type", type);
		if (id != null) {
			EmergencyContactDTO dto = emergencyContactService.load(id);
			map.put("n", dto);
		}
		return "admin/emergencyContact/emergencyContact_detail";
	}

	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(EmergencyContactDTO dto,String iDCard) throws Exception {

		if (dto.getId() == null) {
			UserDTO userDTO = getCurrentUserDTO();
			EmployeeDTO employeeDTO=new EmployeeDTO();
		  if(iDCard!=null)
		  {
				List<EmployeeDTO> list=employeeService.findAll();
				for (EmployeeDTO e:list
						) {

						if(e.getIDCard().equals(iDCard)){
							employeeDTO.setId(e.getId());
						break;}

				}
              if(employeeDTO.getId()==null)
          {
              throw new RuntimeException("您还没有添加员工信息！！");
          }
				dto.setEmployee(employeeDTO);
				emergencyContactService.create(dto);
			}else {
				employeeDTO.setUser(userDTO);
				employeeDTO = employeeService.findOne(employeeDTO);
              if(employeeDTO==null)
              {
                  throw new RuntimeException("您还没有添加员工信息！！");
              }
				dto.setEmployee(employeeDTO);
				emergencyContactService.create(dto);
			}
		} else if(userDTO.getType()==1){
//			throw new RuntimeException("请实现更新逻辑，并删除该行");

			EmergencyContactDTO old = emergencyContactService.load(dto.getId());
			// 确定哪些字段是在详情页面需要修改的，不在详情页修改的，就不需要在这里set值。updated和updatedBy不需要在这里处理，框架会统一处理
			old.setId(dto.getId());
			old.setEmployee(dto.getEmployee());
			old.setName(dto.getName());
			old.setRelation(dto.getRelation());
			old.setPhone(dto.getPhone());
			old.setAddress(dto.getAddress());
			emergencyContactService.updateAllFields(old);

		}else
		{
			throw new RuntimeException("管理员才可以修改！！！");
		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete")
	public String delete(Long[] ids) throws IOException {
		UserDTO dto=getCurrentUserDTO();
		if(dto.getType()!=1)
		{
			throw  new BizException("您没有权限！！");
		}
		emergencyContactService.deleteByIds(ids);
		return ok();
	}

	@RequestMapping(value = "/list")
	public String list(EmergencyContactDTO dto, Boolean search, Map<String, Object> map, Page page) {
		List<EmergencyContactDTO> dtos = emergencyContactService.find(dto, page);
		UserDTO userDTO=getCurrentUserDTO();

		EmployeeDTO employeeDTO=new EmployeeDTO();
		employeeDTO.setUser(userDTO);
		employeeDTO=employeeService.findOne(employeeDTO);
		//判断类型如果不是为管理员，默认只显示自己信息
		if(employeeDTO==null&&userDTO.getType()!=1)
		{
			dtos=null;
		}else if(userDTO.getType()!=1)
		{
			EmergencyContactDTO e;
			if(dtos.size()>0) {
				for (int i = 0; i < dtos.size(); i++) {
					e = dtos.get(i);
					if (e.getEmployee().getId() == employeeDTO.getId()) {
						continue;
					} else {
						dtos.remove(i);
						i--;
					}
				}
			}
		}

		map.put("type", userDTO.getType());
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/emergencyContact/emergencyContact_list";
	}

	@RequestMapping(value = "/search")
	public String search(EmergencyContactDTO dto, Map<String, Object> map, Page page) {
		List<EmergencyContactDTO> dtos = emergencyContactService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/emergencyContact/emergencyContact_search";
	}

}
